package cn.school.demo.repository

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import cn.school.demo.MyApplication
import cn.school.demo.pojo.Hobby
import cn.school.demo.pojo.Result
import cn.school.demo.util.HttpApi
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class SetLoveRepository {
    private val httpApi: HttpApi

    init {
        val retrofit =
            Retrofit.Builder().baseUrl(DataRepository.BASEURL).addConverterFactory(
                GsonConverterFactory.create()
            ).build()
        httpApi = retrofit.create(HttpApi::class.java)
    }

    //保存用户爱好
    fun saveUserLove(id: Int, l: List<Hobby>): LiveData<Result> {
        val responseLiveData = MutableLiveData<Result>()
        val params = mapOf("sfNumber" to id, "Hobby" to GsonUtils.toJson(l))
        httpApi.saveUserLoveList(params).enqueue(object : Callback<Result> {
            override fun onResponse(call: Call<Result>, response: Response<Result>) {
                LogUtils.d("保存用户爱好http回调成功！")
                response.body()?.let {
                    responseLiveData.value = response.body()
                } ?: LogUtils.e("保存用户爱好http返回值为空！")
            }

            override fun onFailure(call: Call<Result>, t: Throwable) {
                LogUtils.e(t)
            }
        })
        return responseLiveData
    }

    //获取用户爱好
    fun getUserLove(id: Int): LiveData<MutableList<Hobby>> {
        val responseLiveData = MutableLiveData<MutableList<Hobby>>()
        val params = mapOf("sfNumber" to id)
        httpApi.getUserLoveList(params).enqueue(object : Callback<Result> {
            override fun onResponse(call: Call<Result>, response: Response<Result>) {
                LogUtils.d("获取用户爱好http回调成功！")
                response.body()?.let {
                    it.data["hobby"]?.let {
                        try {
                            val hobbyList = GsonUtils.fromJson<MutableList<Hobby>>(
                                response.body()!!.data["hobby"].toString(),
                                object : TypeToken<MutableList<Hobby>>() {}.type
                            )
                            LogUtils.d(hobbyList.toString(), hobbyList.size)
                            responseLiveData.value = hobbyList
                        } catch (e: Error) {
                            e.printStackTrace()
                        }
                    } ?: LogUtils.e("获取用户爱好http返回值的data[\"hobby\"]为空！")
                } ?: LogUtils.e("获取用户爱好http返回值为空！")
            }

            override fun onFailure(call: Call<Result>, t: Throwable) {
                LogUtils.e(t)
            }
        })
        return responseLiveData
    }

    //获取全部爱好
    fun getAllLove(id: Int): LiveData<MutableList<Hobby>> {
        val responseLiveData = MutableLiveData<MutableList<Hobby>>()
        val params = mapOf("sfNumber" to id)
        httpApi.getAllLoveList(params).enqueue(object : Callback<Result> {
            override fun onResponse(call: Call<Result>, response: Response<Result>) {
                LogUtils.d("获取全部爱好http回调成功！")
                response.body()?.let {
                    if (response.body()!!.data["hobby"].toString() != "NULL") {
                        try {
                            val hobbyList = GsonUtils.fromJson<MutableList<Hobby>>(
                                response.body()!!.data["hobby"].toString(),
                                object : TypeToken<MutableList<Hobby>>() {}.type
                            )
                            LogUtils.d(hobbyList.toString())
                            responseLiveData.value = hobbyList
                        } catch (e: Error) {
                            e.printStackTrace()
                        }
                    } else {
                        responseLiveData.value = mutableListOf()
                    }
                } ?: LogUtils.e("获取全部爱好http返回值为空！")
            }

            override fun onFailure(call: Call<Result>, t: Throwable) {
                LogUtils.e(t.message)
            }
        })
        return responseLiveData
    }
}